<template>
  <div class="login">
    <div class="login_box">
      <div class="avatat_box">
        <img src="../assets/logo.png" alt />
      </div>
      <el-form ref="loginForm" :model="loginForm" class="login_form" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item style="float:right">
          <el-button type="primary"  @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { async } from 'q';
export default {
  data() {
    return {
      // 表单用到的数据
      loginForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 4, max: 12, message: "长度在 5 到 12 个字符", trigger: "blur" }
        ]
      },
    };
  },
  methods: {
    //   点击重置按钮
      resetForm(){
          //只要调用resrtFields
          this.$refs.loginForm.resetFields()
      },
    //   登录
      login(){
          this.$refs.loginForm.validate(async flag=>{
              console.log(flag);
              if(!flag) return
                console.log('成功');
               var {data:res}=await this.axios.post('login',this.loginForm)
               console.log(res);
               if(res.meta.status==200){
                   this.$mg.success('登录成功')
                   
                   sessionStorage.setItem('token',res.data.token)
                   this.$router.push('/home')
               }else{
                   this.$mg.error(res.meta.msg)
               }
             
          })
      }
  },
  created() {}
};
</script>

<style lang='less' scoped>
.login {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 8px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .avatat_box {
    width: 130px;
    height: 130px;
    border: 1px solid #000;
    background-color: rgb(28, 0, 128);
    border-radius: 50%;
    overflow: hidden;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0px 0px 30px #000;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      border: 1px solid rgb(0, 92, 128);
      background-color: red;
    }
  }
  .login_form {
    width: 100%;
    position: absolute;
    bottom: 0;
    box-sizing: border-box;
    padding: 0 20px;
  }
}
</style>
